Page({
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    appointment: '',
    repairItemName: '',
    mobile: '',
    description: '',
    houseList: [],
    repairItem: [],
    attachment: [],
    houseId: '',
    repairItemId: ''
  },
  onLoad ({ id }) {
    // 获取审核通过的房屋列表
    this.getHouseList()
    // 获取维修项目
    this.getRepairItem()
    if (id) {
      // 更新标题
      wx.setNavigationBarTitle({ title: '修改报修信息' })
      this.getRepairDetail(id)
    }
  },
  // 详情接口
  async getRepairDetail (id) {
    // 没有id直接返回不发请求
    if (!id) return
    const { code, data: repairDetail } = await wx.http.get('/repair/' + id)
    if (code !== 10000) return wx.utils.toast('获取报修详情失败')
    this.setData({
      ...repairDetail
    })
  },
  // 选择房屋
  selectHouse (ev) {
    console.log(ev.detail)
    const { id: houseId, name: houseInfo } = ev.detail
    this.setData({
      houseInfo,
      houseId
    })
  },
  // 选择维修项目
  selectRepairItem (ev) {
    console.log(ev.detail)
    const { id: repairItemId, name: repairItemName } = ev.detail
    this.setData({
      repairItemName,
      repairItemId
    })
  },
  // 选择时间
  selectDate (ev) {
    console.log('选择时间', ev)
    this.setData({
      dateLayerVisible: false,
      appointment: wx.utils.formatDate(ev.detail)
    })
  },
  // 已通过审核房屋列表接口
  async getHouseList () {
    const { code, data: houseList } = await wx.http.get('/house')
    if (code !== 10000) return wx.utils.toast('获取房屋列表失败')
    this.setData({
      houseList
    })
  },
  // 维修项目接口
  async getRepairItem () {
    // 请求接口
    const { code, data: repairItem } = await wx.http.get('/repairItem')
    if (code !== 10000) return wx.utils.toast('获取维修项目失败')
    this.setData({
      repairItem
    })
  },
  // 上传图片
  afterRead (ev) {
    console.log(ev)
    const { file } = ev.detail
    // 调用微信上传
    wx.uploadFile({
      url: wx.http.baseURL + '/upload',
      filePath: file.url,
      name: 'file',
      header: {
        Authorization: getApp().token
      },
      success: (res) => {
        console.log('res', res)
        // 转换json数据
        const data = JSON.parse(res.data)
        const { attachment = [] } = this.data
        attachment.push({ ...data.data })
        this.setData({
          attachment
        })
      }
    })

  },
  openHouseLayer () {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer () {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer () {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer () {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer () {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer () {
    this.setData({ dateLayerVisible: false })
  },
  // goList () {
  //   wx.reLaunch({
  //     url: '/repair_pkg/pages/list/index',
  //   })
  // },
  verifyHouse () {
    const valid = this.data.houseId !== ''
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择房屋信息!')
    // 返回验证结果
    return valid
  },
  verifyRepair () {
    const valid = this.data.repairItemId !== ''
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择维修项目!')
    // 返回验证结果
    return valid
  },
  // 校验函数
  verifyMobile () {
    // 验证手机号
    const reg = /^[1][3-8][0-9]{9}$/
    const valid = reg.test(this.data.mobile.trim())
    // 验证结果提示
    if (!valid) wx.utils.toast('请填写正确的手机号码!')
    // 返回验证结果
    return valid
  },
  verifyDate () {
    // 验证日期格式
    const reg = /^\d{4}\/\d{2}\/\d{2}$/
    const valid = reg.test(this.data.appointment)
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择预约日期!')
    // 返回验证结果
    return valid
  },
  verifyDescription () {
    // 验证报修项目描述
    const valid = this.data.description.trim() !== ''
    // 验证结果提示
    if (!valid) wx.utils.toast('请填写问题描述!')
    // 返回验证结果
    return valid
  },
  async submitForm () {
    // 验证表单数据
    if (!this.verifyHouse()) return
    if (!this.verifyRepair()) return
    if (!this.verifyMobile()) return
    if (!this.verifyDate()) return
    if (!this.verifyDescription()) return
    console.log('可以调接口了')
    const { id, houseId, repairItemId, appointment, mobile, description, attachment } = this.data
    const { code } = await wx.http.post('/repair', { id, houseId, repairItemId, appointment, mobile, description, attachment })
    if (code !== 10000) return wx.utils.toast('在线报修失败')
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index'
    })
  },
  //删除图片
  delPicture (ev) {
    console.log(ev)
    // [{id:1},{id:2 }, {id:3}]  当前点了2
    // 会返回 [{id:1},{id:3}]
    const list = this.data.attachment.filter(item => item.id !== ev.detail.file.id)

    this.setData({
      attachment: list
    })
  }
})
